package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.bean.MyAddressBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/11.
 */

public class MyAddressAdapter extends RecyclerArrayAdapter<MyAddressBean.DataBean> {


    private addressInterFace addressInterfaceListener;

    public void editAddressInterfaceListener(addressInterFace listener){
        addressInterfaceListener = listener;

    }
    public interface addressInterFace {
        /**
         * 设置默认地址
         * @param position
         */
        void  setDefaultAddress(int position);

        /**
         * 编辑收货地址
         * @param position
         */
        void  editAddress(int position);
    }

    public MyAddressAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyAddressHolder(parent);
    }


    class MyAddressHolder extends BaseViewHolder<MyAddressBean.DataBean> {


        @Bind(R.id.tv_delete)
        TextView tvDelete;
        @Bind(R.id.rowBG)
        LinearLayout rowBG;
        @Bind(R.id.name_and_phone)
        TextView nameAndPhone;
        @Bind(R.id.img_default)
        ImageView imgDefault;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.ll_content)
        LinearLayout llContent;
        @Bind(R.id.img_edit)
        ImageView imgEdit;
        @Bind(R.id.rowFG)
        LinearLayout rowFG;

        public MyAddressHolder(ViewGroup parent) {
            super(parent, R.layout.item_my_address);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(MyAddressBean.DataBean data) {
            super.setData(data);

            nameAndPhone.setText(data.name+" "+data.mobile);
            address.setText(data.province+data.city+data.county+data.address);
            if (data.is_default.equals("1")){
                imgDefault.setImageResource(R.drawable.ic_default_address_no);
            }else {
                imgDefault.setImageResource(R.drawable.ic_default_address);
            }

            imgEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (addressInterfaceListener!=null) {
                        addressInterfaceListener.editAddress(getDataPosition());
                    }
                }
            });
            imgDefault.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (addressInterfaceListener!=null) {
                        addressInterfaceListener.setDefaultAddress(getDataPosition());
                    }
                }
            });
        }
    }

}
